Data types define the type of value a variable can hold.
Data types can be categorized into two groups:
Primitive Data Type are simple/basic and cannot be change once assigned (Immutable).
There are 7 types of Primitive Data Types in JavaScript, They are: -
The Number type represent both integer and floating-point numbers.
let a = 10;
let b = 3.14;
console.log(a);
console.log(b);
Output:
10
3.14
String is a sequence of characters enclosed with single quotes(' '), double quotes(" "), or backticks(` `).
let Fname = 'Shivam'; // Single Quotes
console.log(Fname);
let Lname = "Maurya"; // Double Quotes
console.log(Lname );
let Fullname = `${Fname} ${Lname}`; // backticks/Template literal
console.log(Fullname );
Output:
Shivam
Maurya
Shivam Maurya
Boolean Represents one of two values — either true
or false
.
let isOnline = true;
console.log(isOnline);
let isOffline = false;
console.log(isOffline);
Output:
true
false
A variable that is declared but not assigned a value has type undefined
.
let x;
console.log(x);
Output:
undefined
A null Value represents an empty or Unknown value.
let data = null;
console.log(data);
Output:
null
A Symbol is a unique and immutable primitive value that is mainly used to create unique property keys.
Symbols are always unique. It is Introduce in ES6 Update.
let id = Symbol("id");
console.log(id);
Output:
Symbol(id)
BigInt is used to store very large integers beyond the safe limit of the Number type.
It is useful for Handling large numbers in cryptography, Precise calculations, ets.
let bigNumber = 1234567890123456789012345678901234567890n;
console.log(bigNumber);
Output:
1234567890123456789012345678901234567890n
Non-primitive data types is a type of Data Type in JavaScript.
It is used to store the reference (Address of memory) instead of actual value.
Non-Primitive Data type can hold more than one value and can be change after creation. It is stored in heap memory and accessed by reference.
There are 3 types of Non-Primitive Data Types in JavaScript, They are: -
An Object is a Non-Primitive data type.
Object is like a container or box that holds Information or Data in key value pair.
Where key is like a label (a property name) and value is data assigned to that key.
Key are always string (It can be or can not be written in Quotes) and Value can be any data type (String, number, boolean, array, function, or even another object).
let person = {
name: "Shivam",
age: 21,
Dob: "2002-10-02"
};
console.log(person);
Output:
{
name: 'Shivam',
age: 21,
Dob: '2002-10-02'
}
An Array is a special type of variable or Object that can hold multiple values in a single place. Instead of creating separate variable for each values.
We can use an Array to store them in an ordered list.
let fruits = ["Apple", "Banana", "Orange"];
console.log(fruits);
Output:
[ 'Apple', 'Banana', 'Orange' ]
A function is a reusable block of code that performs a specific tasks.
Or A function is a set of code that can be executed multiple time.
Instead of writing the same code again and again, we can write a function once and use it whenever we needed.
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Shivam"));
Output:
Hello, Shivam!
To check if a variable is a function, you can use the typeof
operator.
Functions are objects, but they're given special handling by JavaScript, so the typeof operator returns "function" for them instead of "object".
let myFunction = function() {
return "Hello, World!";
};
console.log(typeof myFunction);
Output:
function
Even though arrays are objects in JavaScript, we can check if a variable is an array using Array.isArray()
.
let myArray = [1, 2, 3];
console.log(typeof myArray);
console.log(Array.isArray(myArray));
Output:
object
true